package com.coocaa.ops.admin.v1.todotask.provider.impl;

import com.coocaa.ops.admin.v1.audit.mapper.IAuditTaskInfoMapper;
import com.coocaa.ops.admin.v1.todotask.model.AuditTaskInfoModel;
import com.coocaa.ops.admin.v1.todotask.model.TaskPageListModel;
import com.coocaa.ops.admin.v1.todotask.provider.ITaskQueryProvider;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.validator.ResourceTypeValidator;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author bijiahao
 * @date : 2018/12/10.
 * @description
 */
@Service
public class TaskQueryProviderImpl implements ITaskQueryProvider {
    @Autowired
    private IAuditTaskInfoMapper auditTaskInfoMapper;

    @Override
    public int getTotalTask(Map<String, Object> params) {
        Integer total = auditTaskInfoMapper.getTotalTask(params);
        return total == null ? 0 : total;
    }

    @Override
    public PageResultModel<TaskPageListModel> listTaskPage(AuditTaskInfoModel model, Integer userId) {
        Map<String, Object> params = getQueryParams(model, userId);
        Integer total = auditTaskInfoMapper.getTotalTask(params);
        List<TaskPageListModel> list = InstanceUtil.newArrayList(0);
        if (total != null && total > 0) {
            list = listTaskPage(model.getResourceType(), params);
        }
        return PageResultModel.getPageResultModel(total, list);
    }


    private List<TaskPageListModel> listTaskPage(String resourceType, Map<String, Object> params) {
        if (ResourceTypeValidator.isTab(resourceType)) {
            return auditTaskInfoMapper.listTabTaskPage(params);
        } else if (ResourceTypeValidator.isPanel(resourceType)) {
            return auditTaskInfoMapper.listPanelTaskPage(params);
        } else {
            return auditTaskInfoMapper.listCommonTaskPage(params);
        }
    }


    private Map<String, Object> getQueryParams(AuditTaskInfoModel model, Integer userId) {
        Map<String, Object> map = InstanceUtil.newHashMap();
        if (model.getSubmitTaskIs() != null && model.getSubmitTaskIs()) {
            map.put("userId", userId);
        }
        map.put("resourceType", model.getResourceType());
        if (model.getAuditFlag() != null) {
            map.put("auditFlag", model.getAuditFlag());
        }
        if (StringUtils.isNotBlank(model.getTaskName())) {
            map.put("taskName", model.getTaskName());
        }
        if (StringUtils.isNotBlank(model.getSource())) {
            map.put("source", model.getSource());
        }
        if (model.getTaskId() != null) {
            map.put("taskId", model.getTaskId());
        }
        if (StringUtils.isNotBlank(model.getOrder())) {
            map.put("order", model.getOrder());
        }
        if (model.getCurrentOperator() != null) {
            if (Integer.valueOf(0).equals(model.getCurrentOperator())) {
                map.put("currentOperator", userId);
            } else if (Integer.valueOf(1).equals(model.getCurrentOperator())) {
                map.put("others", 1);
                map.put("currentOperator", userId);
            }
        }
        //分页查询参数
        int offset = (model.getPage() - 1) * model.getRows();
        map.put("offset", offset);
        map.put("limit", model.getRows());
        return map;
    }
}
